package com.example.armmvvm.ui.statemachine

import android.os.Looper
import android.os.Message
import com.example.armmvvm.databinding.ActivityStateMachineBinding
import com.example.armmvvm.ui.statemachine.base.State
import com.example.armmvvm.ui.statemachine.base.StateMachine

/**
 *  author : yanghaozhang
 *  date : 2022/4/28 10:09
 *  description :
 */
class OrderStateMachine(
    name: String,
    looper: Looper,
    private val binding: ActivityStateMachineBinding
) : StateMachine(name, looper) {
    internal companion object {
        private const val CMD_ORDER_BASE = 0x00026000

        // 创建订单
        const val CMD_ORDER_CREATE = CMD_ORDER_BASE + 1

        // 付款成功
        const val CMD_ORDER_PAY_SUCCESS = CMD_ORDER_BASE + 2

        // 取消订单
        const val CMD_ORDER_CANCEL = CMD_ORDER_BASE + 3

        // 商家接单
        const val CMD_ORDER_MERCHANT_ASSENT_ORDER = CMD_ORDER_BASE + 4

        // 商家不接单
        const val CMD_ORDER_MERCHANT_REFUSE = CMD_ORDER_BASE + 5

        // 制造完成
        const val CMD_ORDER_WORK_DONE = CMD_ORDER_BASE + 6

        // 配送成功
        const val CMD_ORDER_DELIVERY_SUCCESS = CMD_ORDER_BASE + 7

        // 评价完成
        const val CMD_ORDER_EVALUATE = CMD_ORDER_BASE + 8

        // 退款失败
        const val CMD_ORDER_REFUND_FAIL = CMD_ORDER_BASE + 9

        // 退款成功
        const val CMD_ORDER_REFUND_SUCCESS = CMD_ORDER_BASE + 10

        // 更新界面
        const val CMD_ORDER_REFRESH_VIEW = CMD_ORDER_BASE + 11

        // 重置
        const val CMD_ORDER_RESET = CMD_ORDER_BASE + 12
    }

    private val stateDefault = DefaultState()

    // 代付款
    private val statePrePayment = PrePaymentState()

    // 待商家确认,已付款
    private val statePaid = PendingMerchantConfirmState()

    // 待制作
    private val statePendingWorkDone = PendingWorkDoneState()

    // 待配送
    private val statePendingDelivery = PendingDeliveryState()

    // 待评价
    private val statePendingEvaluated = PendingEvaluatedState()

    // 待退款
    private val statePendingRefund = PendingRefundState()

    // 订单关闭
    private val stateOrderClosed = OrderClosedState()

    // 订单完成
    private val stateOrderCompleted = OrderCompletedState()


    /**
     * addState(stateDefault, null)
     *     addState(statePrePayment, stateDefault)
     *         addState(statePaid, statePrePayment)
     *             addState(statePendingWorkDone, statePaid)
     *                 addState(statePendingDelivery, statePendingWorkDone)
     *                     addState(statePendingEvaluated, statePendingDelivery)
     *             addState(statePendingRefund, statePaid)
     *     addState(stateOrderCompleted, stateDefault)
     *     addState(stateOrderClosed, stateDefault)
     */
    init {
        // CHECKSTYLE:OFF IndentationCheck
        addState(stateDefault, null)
        addState(statePrePayment, stateDefault)
        addState(statePaid, statePrePayment)
        addState(statePendingWorkDone, statePaid)
        addState(statePendingDelivery, statePendingWorkDone)
        addState(statePendingEvaluated, statePendingDelivery)
        addState(statePendingRefund, statePaid)
        addState(stateOrderCompleted, stateDefault)
        addState(stateOrderClosed, stateDefault)
        // CHECKSTYLE:ON IndentationCheck
        setInitialState(stateDefault)
        binding.apply {
            stateCreate.setOnClickListener {
                sendMessage(CMD_ORDER_CREATE)
            }
            statePaySuccess.setOnClickListener {
                sendMessage(CMD_ORDER_PAY_SUCCESS)
            }
            stateCancel.setOnClickListener {
                sendMessage(CMD_ORDER_CANCEL)
            }
            stateMerchantAssentOrder.setOnClickListener {
                sendMessage(CMD_ORDER_MERCHANT_ASSENT_ORDER)
            }
            stateWorkDone.setOnClickListener {
                sendMessage(CMD_ORDER_WORK_DONE)
            }
            stateDeliverySuccess.setOnClickListener {
                sendMessage(CMD_ORDER_DELIVERY_SUCCESS)
            }
            stateEvaluate.setOnClickListener {
                sendMessage(CMD_ORDER_EVALUATE)
            }
            stateMerchantRefuse.setOnClickListener {
                sendMessage(CMD_ORDER_MERCHANT_REFUSE)
            }
            stateRefundFail.setOnClickListener {
                sendMessage(CMD_ORDER_REFUND_FAIL)
            }
            stateRefundSuccess.setOnClickListener {
                sendMessage(CMD_ORDER_REFUND_SUCCESS)
            }
            stateReset.setOnClickListener {
                sendMessage(CMD_ORDER_RESET)
            }
        }
    }

    override fun onPreHandleMessage(msg: Message?) {
        when (msg?.what) {
            CMD_ORDER_RESET -> {
                transitionTo(stateDefault)
            }
            else -> {
                if (!hasMessages(CMD_ORDER_REFRESH_VIEW)) {
                    deferMessage(handler.obtainMessage(CMD_ORDER_REFRESH_VIEW))
                }
            }
        }
    }

    override fun onPostHandleMessage(msg: Message?) {
        super.onPostHandleMessage(msg)
    }

    open class OrderState() : State() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            return super.processMessage(msg)
        }
    }

    /**
     * 默认状态
     */
    inner class DefaultState() : OrderState() {

        override fun enter() {
            super.enter()
            binding.tvState.text = "等待订单中"
        }

        override fun exit() {
            super.exit()
        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_CREATE -> transitionTo(statePrePayment)
                CMD_ORDER_REFRESH_VIEW -> binding.tvState.text = "等待订单中"
            }
            return HANDLED
        }
    }

    /**
     * 代付款
     */
    inner class PrePaymentState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_PAY_SUCCESS -> transitionTo(statePaid)
                CMD_ORDER_CANCEL -> transitionTo(stateOrderClosed)
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "代付款"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 待商家确认
     */
    inner class PendingMerchantConfirmState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_MERCHANT_ASSENT_ORDER -> transitionTo(statePendingWorkDone)
                CMD_ORDER_MERCHANT_REFUSE -> transitionTo(statePendingRefund)
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "待商家确认"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 待制作
     */
    inner class PendingWorkDoneState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_WORK_DONE -> transitionTo(statePendingDelivery)
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "待制作"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 待配送
     */
    inner class PendingDeliveryState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_DELIVERY_SUCCESS -> transitionTo(statePendingEvaluated)
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "待配送"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 待评价
     */
    inner class PendingEvaluatedState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_EVALUATE -> transitionTo(stateOrderCompleted)
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "待评价"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 待退款
     */
    inner class PendingRefundState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_REFUND_SUCCESS -> transitionTo(stateOrderClosed)
                CMD_ORDER_REFUND_FAIL -> transitionTo(statePaid)
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "待退款"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 订单关闭
     */
    inner class OrderClosedState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "订单关闭"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }

    /**
     * 订单完成
     */
    inner class OrderCompletedState() : OrderState() {

        override fun enter() {

        }

        override fun exit() {

        }

        override fun processMessage(msg: Message): Boolean {
            when (msg.what) {
                CMD_ORDER_REFRESH_VIEW -> {
                    binding.tvState.text = "订单完成"
                }
                else -> {
                    return NOT_HANDLED
                }
            }
            return HANDLED
        }
    }
}